package com.situ.object.utils;

import java.sql.*;

public class JdbcUtil {
    //为什么需要private static final修饰 且变量名大写
    private static final String DRIVERNAME = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/manage_system" +
            "?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

    public static void main(String[] args) {

    }

    //创建连接  返回的是连接对象
    public static Connection getConnection(){

        Connection connection = null;

        try {
            Class.forName(DRIVERNAME);
            //2. 创建连接对象
            //格式：jdbc:mysql://地址:端口号/数据库名?参数列表(&连接)

            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            return connection;
        }
    }

    //关闭资源
    public static void close(Connection connection, ResultSet resultSet, PreparedStatement preparedStatement){
        try {
            if (resultSet != null)
                resultSet.close();
            if (preparedStatement != null)
                preparedStatement.close();
            if (connection != null)
                connection.close();


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

}
